//
//  CardDataProcess.m
//  QRMaster
//
//  Created by lc on 16/5/29.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "CardDataProcess.h"
//#import "ItemModel.h"
#import "BulidingModel.h"
#import "FloorsModel.h"
#import "RoomsModel.h"
#import "CardInfoModel.h"
@interface CardDataProcess()
{
    NSMutableArray * bulidingArray;
    NSInteger roomSelect;
    NSInteger roomSum;
}
@end

@implementation CardDataProcess
-(NSMutableArray *)dataProcess:(NSArray *)data
{
    bulidingArray = [[NSMutableArray alloc]init];
    NSMutableArray * floorArray = [[NSMutableArray alloc]init];
    NSMutableArray * roomArray = [[NSMutableArray alloc]init];
    for (NSDictionary * dict in data) {
        roomSelect = 0;
        roomSum = 0;
        ItemModel * bmodel = [[ItemModel alloc]init];
        bmodel.itemId = [dict objectForKey:@"buildingid"];
        bmodel.itemNo = [dict objectForKey:@"buildingno"];
        bmodel.itemName = [dict objectForKey:@"buildingname"];
        bmodel.itemChildrenArray = [dict objectForKey:@"floors"];
        bmodel.nodeLevel = 1;
        bmodel.nodeType = NodeTypeGroup;
        bmodel.isClick = NO;
        bmodel.nodeId = [dict objectForKey:@"buildingid"];
        if (bmodel.itemChildrenArray) {
            [floorArray removeAllObjects];
            for (NSDictionary * fdict in bmodel.itemChildrenArray) {
                ItemModel * fmodel = [[ItemModel alloc]init];
                fmodel.itemId = [fdict objectForKey:@"floorid"];
                fmodel.itemName = [fdict objectForKey:@"floorname"];
                fmodel.itemNo = [fdict objectForKey:@"floorno"];
                fmodel.itemChildrenArray = [fdict objectForKey:@"rooms"];
                fmodel.nodeLevel = 2;
                fmodel.isClick = NO;
                fmodel.nodeType = NodeTypeGroup;
                fmodel.nodeId = [dict objectForKey:@"floorid"];
                if (fmodel.itemChildrenArray) {

                    [roomArray removeAllObjects];
                    for (NSDictionary * rdict in fmodel.itemChildrenArray) {
                        RoomsModel * rmodel = [[RoomsModel alloc]init];
                        rmodel.roomId = [rdict objectForKey:@"roomid"];
                        rmodel.roomName = [rdict objectForKey:@"roomname"];
                        rmodel.roomNo = [rdict objectForKey:@"roomno"];
                        rmodel.isSelect = [rdict objectForKey:@"isSelected"];//是否被分配的房间
                        rmodel.nodeLevel = 3;
                        rmodel.nodeType = NodeTypeItem;
                        rmodel.nodeId = [dict objectForKey:@"roomid"];
                        roomSum ++;
                        if ([rmodel.isSelect isEqualToString:@"1"]) {
                            fmodel.itemSelect ++;
//                            [allocatedRoomArray addObject:rmodel.roomid];
                            roomSelect ++;
                        }
                        fmodel.itemSum ++;
                        [roomArray addObject:rmodel];
                    }
                    if (roomArray.count > 0) {
                        bmodel.itemSum ++;
                        if (fmodel.itemSelect > 0) {
                            bmodel.itemSelect ++;
                        }
                        fmodel.itemChildrenArray = [roomArray copy];
                        [floorArray addObject:fmodel];
                    }
                }
                
            }
            if (floorArray.count > 0) {
                bmodel.itemChildrenArray = [floorArray copy];
                [bulidingArray addObject:bmodel];
            }
        }
    }
    return bulidingArray;
}

#pragma mark -------获取新模型的房间
-(NSMutableArray *)getRoomDataAllocate
{
    NSMutableArray * roomArray = [NSMutableArray array];
    for (ItemModel * bModel in bulidingArray) {
        for (ItemModel * fModel in bModel.itemChildrenArray) {
            for (RoomsModel * rModel in fModel.itemChildrenArray) {
                RoomsModel * rTModel = [[RoomsModel alloc]init];
                rTModel.roomId = rModel.roomId;
                rTModel.roomNo  = rModel.roomNo;
                rTModel.roomName = rModel.roomName;
//                rTModel.bulidingName = bModel.itemName;
//                rTModel.floorName = fModel.itemName;
//                rTModel.timeArray = rModel.timeArray;
                [roomArray addObject:rTModel];
            }
        }
    }
    return roomArray;
}

#pragma mark ------获取新模型的楼层
-(NSMutableArray *)getFloorDataAllocate
{
    NSMutableArray * floorArray = [NSMutableArray array];
    for (ItemModel * bModel in bulidingArray) {
        for (ItemModel * fModel in bModel.itemChildrenArray) {
            FloorsModel * model = [[FloorsModel alloc]init];
            model.floorId = fModel.itemId;
            model.floorName = fModel.itemName;
//            model.bulidingName = bModel.itemName;
//            model.selectRoom = fModel.itemSelect;
//            model.totalRoom = fModel.itemSum;
            [floorArray addObject:model];
        }
    }
    return floorArray;
}

#pragma mark ------获取新模型的楼栋
-(NSMutableArray *)getBuildDataAllocate
{
    NSMutableArray * buildArray = [NSMutableArray array];
    for (ItemModel * bModel in bulidingArray) {
        BulidingModel * model = [[BulidingModel alloc]init];
        model.buildingId = bModel.itemId;
        model.buildingNo = bModel.itemNo;
        model.buildingName = bModel.itemName;
//        model.sumRoom = bModel.itemSum;
//        model.floorArray = bModel.itemChildrenArray;
        [buildArray addObject:model];
    }
    return buildArray;
}
@end
